package com.s2paganini.tween 
{
	import flash.display.DisplayObject;	import flash.events.TimerEvent;	import flash.utils.Timer;	
	/**
	 * @author siLvio paganini | s2paganini.com
	 * @purpose Blinks the Display Object
	 */

	public class Flick 
	{
		private static var timer : Timer;
		private static var objectToFlick : DisplayObject;
		private static var visibility : Boolean;
		private static var sequence : Array;
		private static var functionToApply : Function;
		private static var delay : Number;
		private static var timerStart : Timer;
		private static var repeat : Number;
		private static var order : Number;
		private static var functionParams : Array;
		private static var interval : Number;
		
		/*
		 * <b>Parameters</b><br>
		 * @param obj DisplayObject to tween
		 * @param visible Set visibility of the DisplayObject after it ends (<b>Default : true</b>)
		 * @param sequence Array that sets the pattern of visibility (<b>Default : [0, 1]</b>)
		 * @param repeat Number of times that it should repeat the sequence (<b>Default : 1</b>)
		 * @param delay Delay to start Tweening ((<b>Default : 0</b>)
		 * @param interval Interval between each blink in milliseconds (<b>Default : 30</b>)
		 * @param onComplete function to call after Tween ends
		 * @param onCompleteParams Array of params of the onComplete function
		 * 
		 * @usage
		 * Flick.addFlick(displayObject, {params : Object]});
		 */

		public static function addFlick(obj : DisplayObject, p_param : Object) : void
		{
			objectToFlick = obj;
			sequence = new Array();
			functionParams = new Array();
			
			visibility = (p_param["visible"] == null) ? true : p_param["visible"];
			sequence = (p_param["sequence"] == null) ? [0, 1] : p_param["sequence"];
			repeat = (p_param["repeat"] == null) ? 1 : p_param["repeat"];
			functionToApply = (p_param["onComplete"] == null) ? null : p_param["onComplete"];
			functionParams = (p_param["onCompleteParams"] == null) ? null : p_param["onCompleteParams"];
			delay = (p_param["delay"] == null) ? 0 : p_param["delay"];
			interval = (p_param["interval"] == null) ? 30 : p_param["interval"];
			
			timer = new Timer(interval, repeat * sequence.length);
			timer.addEventListener(TimerEvent.TIMER, listenerTimer);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, listenerTimer);
			
			timerStart = new Timer(delay * 1000, 1);
			timerStart.addEventListener(TimerEvent.TIMER_COMPLETE, listenerTimerStart);
			timerStart.start();
		}
		
		private static function listenerTimerStart(event : TimerEvent) : void
		{
			timerStart.removeEventListener(TimerEvent.TIMER_COMPLETE, listenerTimerStart);
			timer.start();
			order = 0;
		}

		private static function listenerTimer(event : TimerEvent) : void
		{
			switch(event.type) {
				case "timer":
					objectToFlick.visible = sequence[order];
					order++;
					if(order == sequence.length - 1) order = 0;
					break;
				
				case "timerComplete":
					timer.removeEventListener(TimerEvent.TIMER, listenerTimer);
					timer.removeEventListener(TimerEvent.TIMER_COMPLETE, listenerTimer);
					if(functionToApply != null) functionToApply.apply(null, functionParams);
					objectToFlick.visible = visibility;
					break;	
			}
		}
	}
}
